package base.tree.search.preOrder;

import base.tree.TreeNode;

import java.util.Stack;

/**
 * 二叉树先序遍历
 */
public class Solution {

    // 非递归先序遍历
    public void preOrder(TreeNode node){

        Stack<TreeNode> stack = new Stack<>();

        while (!stack.isEmpty()||node!=null){
            while(node!=null){
                System.out.println(node.val);
                stack.push(node);
                node=node.left;
            }
            if(!stack.isEmpty()){
                node=stack.pop();
                node=node.right;
            }
        }
    }

    // 递归先序遍历
    public void preOrderRe(TreeNode node){
        if (node==null){
            return;
        }else{
            System.out.println(node.val);
            preOrderRe(node.left);
            preOrderRe(node.right);
        }

    }
}
